package me.yobol.lintcode.medium.leftrotatebits;

/**
 *
 * @author Yobol
 */
public class LeftRotateBits implements ILeftRotateBits{

   /*
     << 左移，右端补0
     >> 带符号位右移
     >>>不论最左边是什么数，都补0
    */
    @Override
    public int leftRotate(int n, int d) {
        d %= 32;
        /*
            n = 123    d = 4
            n = 00000000 00000000 00000000 01111111
            a = n << 4 = 00000000 00000000 00000111 11110000
            b = n >>> 28 = 00000000 00000000 00000000 00000000
            res = a | b = 00000000 00000000 00000111 11110000
        */
        int res = (n << d) | (n >>> (32 - d));
        return res;
    }
    
}
